import requests
from beautifultable import BeautifulTable


def get_ncov_info(url):
    try:
        response = requests.get(url)
        data = response.json()
        return data
    except:
        return None

cityname = input("请输入省市名称")
url = f'https://lab.isaaclin.cn//nCoV/api/area?latest=1&province={cityname}'
data = get_ncov_info(url)

if data == None or not data['results']:
    print("没有爬取到数据")
else:
    currentConfirmedCount = data['results'][0]['currentConfirmedCount']
    confirmedCount = data['results'][0]['confirmedCount']
    suspectedCount = data['results'][0]['suspectedCount']
    curedCount = data['results'][0]['curedCount']
    deadCount = data['results'][0]['deadCount']
    table = BeautifulTable()
    table.column_headers = ['现存确诊人数','累计确诊人数','疑似感染人数','治愈人数','死亡人数']
    table.append_row([currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount])
    print(table)

